/**
 * Created by Dell on 2017/5/16.
 */
'use strict';
const pg = require('../lib/pgClient');
const log = require('log4js').getLogger('experience');
const async = require('async');
let sqlStart = "START TRANSACTION;";
let sqlRollback = "ROLLBACK;";
let sqlCommit = "COMMIT;";
/**
 * 添加现场
 * @param params//添加的参数
 * @param callback
 */
//添加的sql语句
const add_sql1 = 'SELECT * FROM users WHERE id = $1';//有没有用户
const add_sql5 = 'SELECT id FROM experience WHERE title = $1';//查重名现场
const add_sql2 = 'INSERT INTO experience (title, "desc", status, want, own, createtime, starttime, del, imgurl, userid, username, path, point) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13) RETURNING id';
const add_sql6 = 'UPDATE users SET e_wantnum = $1 WHERE id = $2';//修改用户想要现场数
const add_sql7 = 'UPDATE users SET e_ownnum = $1 WHERE id = $2';//修改用户拥有现场数
const add_sql8 = 'INSERT INTO ownlogs (pid, userid, createtime, type) VALUES ($1, $2, $3, $4)';//添加拥有记录
const add_sql9 = 'INSERT INTO wantlogs (pid, userid, createtime, type) VALUES ($1, $2, $3, $4)';//添加想要记录
exports.add = function(params, args,  user, callback) {
    var the_user;
    const params1 = [
        user.id
    ];
    const params5 = [
        args.title
    ];
    pg.connect(function (err, client, done) {
        client.query(sqlStart, function () {
            client.query(add_sql1, params1, function (err, result) {//查作者
                if (err) {
                    log.error(err);
                    client.query(sqlRollback, function (errRollback) {
                        if (errRollback) {
                            log.error('查找作者失败');
                        }
                        done();
                        callback(err, null);
                    })
                } else if (result.rowCount == 0) {
                    log.error('没有此用户');
                    client.query(sqlRollback, function (errRollback) {
                        if (errRollback) {
                            log.error('没有此用户');
                        }
                        done();
                        return callback(err,'null');
                    })
                } else {
                    the_user = result.rows[0];
                    client.query(add_sql5, params5, function (err, result) {//查重名
                        if (err) {
                            log.error(err);
                            client.query(sqlRollback, function (errRollback) {
                                if (errRollback) {
                                    log.error('查找重名失败');
                                }
                                done();
                                callback(err, null);
                            })
                        } else if (result.rowCount > 0) {
                            log.error('有重名现场');
                            client.query(sqlRollback, function (errRollback) {
                                if (errRollback) {
                                    log.error('有重名现场');
                                }
                                done();
                                return callback(err, 'exit_title');
                            })
                        } else {
                            client.query(add_sql2, params, function (err, result) {//添加现场
                                if (err || result.rowCount == 0) {//失败
                                    log.error(err);
                                    client.query(sqlRollback, function (errRollback) {
                                        if (errRollback) {
                                            log.error('添加现场失败');
                                        }
                                        done();
                                        callback(err,null);
                                    });
                                } else {
                                    let experience = result.rows[0];
                                    const params8 = [
                                        experience.id,
                                        the_user.id,
                                        new Date(),
                                        3
                                    ];
                                    const params9 = [
                                        experience.id,
                                        the_user.id,
                                        new Date(),
                                        3
                                    ];
                                        if(args.want){
                                            client.query(add_sql6, [(the_user.p_wantnum + 1),the_user.id], function (err, result) {//修改用户想要
                                                if (err || result.rowCount == 0) {
                                                    log.error(err);
                                                    client.query(sqlRollback, function (errRollback) {
                                                        if (errRollback) {
                                                            log.error('添加推荐失败');
                                                        }
                                                        done();
                                                        callback(err, null);
                                                    });
                                                } else {
                                                    client.query(add_sql8, params8, function (err, result) {//添加用户想要记录
                                                        if (err || result.rowCount == 0) {
                                                            log.error(err);
                                                            client.query(sqlRollback, function (errRollback) {
                                                                if (errRollback) {
                                                                    log.error('添加用户想要记录失败');
                                                                }
                                                                done();
                                                                callback(err, null);
                                                            });
                                                        } else {
                                                            client.query(sqlCommit, function (errCommit) {
                                                                if (errCommit) {
                                                                    client.query(sqlRollback, function (errRollback) {
                                                                        if (errRollback) {
                                                                            log.error('回滚失败');
                                                                        }
                                                                        done();
                                                                        callback(err, null);
                                                                    });
                                                                } else {
                                                                    done();
                                                                    return callback(err, result);//修改成功
                                                                }
                                                            });
                                                        }
                                                    });
                                                }
                                            });
                                        }else if(args.own){
                                            client.query(add_sql7,  [(the_user.p_ownnum + 1),the_user.id], function (err, result) {//修改用户拥有
                                                if (err || result.rowCount == 0) {
                                                    log.error(err);
                                                    client.query(sqlRollback, function (errRollback) {
                                                        if (errRollback) {
                                                            log.error('修改用户拥有失败');
                                                        }
                                                        done();
                                                        callback(err, null);
                                                    });
                                                } else {
                                                    client.query(add_sql9, params9, function (err, result) {//添加用户拥有记录
                                                        if (err || result.rowCount == 0) {
                                                            log.error(err);
                                                            client.query(sqlRollback, function (errRollback) {
                                                                if (errRollback) {
                                                                    log.error('添加用户拥有记录失败');
                                                                }
                                                                done();
                                                                callback(err, null);
                                                            });
                                                        } else {
                                                            client.query(sqlCommit, function (errCommit) {
                                                                if (errCommit) {
                                                                    client.query(sqlRollback, function (errRollback) {
                                                                        if (errRollback) {
                                                                            log.error('回滚失败');
                                                                        }
                                                                        done();
                                                                        callback(err, null);
                                                                    });
                                                                } else {
                                                                    done();
                                                                    return callback(err, result);//修改成功
                                                                }
                                                            });
                                                        }
                                                    });
                                                }
                                            });
                                        } else{
                                            client.query(sqlCommit, function (errCommit) {
                                                if (errCommit) {
                                                    client.query(sqlRollback, function (errRollback) {
                                                        if (errRollback) {
                                                            log.error('回滚失败');
                                                        }
                                                        done();
                                                        callback(err, null);
                                                    });
                                                } else {
                                                    done();
                                                    return callback(err, result);//修改成功
                                                }
                                            });

                                    }
                                }
                            });
                        }
                    });
                }
            });
        });
    })
};

/**
 * 后台文章多条件查询列表
 * @param params
 * @param args
 * @param callback
 */
exports.list_b = function(params, args, callback){
    let sql = 'SELECT * FROM experience WHERE 1=1';//简单编辑sql
    let number = 1;
    if(args.keyword){//对图片名称、用户名、描述进行模糊查询
        sql +=' AND ( title like $'+number+' OR "desc" like $'+number+' OR username like $'+number+' )';
        number ++;
    }
    sql += ' AND del = $'+ number;
    if(args.sotring == 'createtime'){//发布时间（倒序排列）
        sql +=' ORDER BY createtime DESC';
    }
    if(args.sotring == 'want'){//想要（倒序排列）
        sql +=' ORDER BY want DESC';
    }
    if(args.sotring == 'own'){//拥有（倒序排列）
        sql +=' ORDER BY own DESC';
    }
    sql +=' LIMIT $'+ (number + 1) +' OFFSET  $'+(number + 2);//添加排序、分页
    pg.query(sql, params, function(err, result){//执行sql语句
        if(err){
            callback (err, null);//报错
        }else{
            callback (err, result);//执行结束
        }
    });
};
/**
 * 后台文章多条件查询列表数据统计
 * @param params
 * @param args
 * @param callback
 */
exports.count_b = function(params, args, callback){
    let sql = 'SELECT * FROM experience WHERE 1=1';//简单编辑sql
    let number = 1;
    if(args.keyword){//对图片名称、用户名、描述进行模糊查询
        sql +=' AND ( title like $'+number+' OR "desc" like $'+number+' OR username like $'+number+' )';
        number ++;
    }
    sql += ' AND del = $'+ number;
    pg.query(sql, params, function(err, result){//执行sql语句
        if(err){
            callback (err, null);//报错
        }else{
            callback (err, result);//执行结束
        }
    });
};
/**
 * 批量编辑现场状态
 */
exports.upd_status = function(args, callback){
    let ids = args.ids;//ids 是数组
    let theids = "'"+ids.join("','")+"'";//将数组ids整理成字符串
    const upd_sql = 'UPDATE experience SET status = $1 WHERE id IN ('+theids+')';//编辑id在theids的现场数据
    const params =[
        args.status
    ]
    pg.query(upd_sql, params, function(err,result){//执行编辑操作
        if(err){//报错
            callback (err,null);
        }else {
            callback (err,result);//执行结束
        }
    });
};

/**
 * 后台批量删除文章
 * @param callback
 */
exports.remove = function(args, callback){
    let ids = args.ids;//ids 是数组
    let theids = "'"+ids.join("','")+"'";//将数组ids整理成字符串
    const sql1 = 'SELECT * FROM experience WHERE id in ('+theids+') AND del = $1';//是否有已经被删除的体验
    const sql2 = 'UPDATE experience SET del = $1 WHERE id in ('+theids+') RETURNING userid, del';//删除体验
    const sql6 = 'DELETE FROM ownlogs WHERE pid in ('+theids+') AND type = $1 RETURNING userid';//删除拥有记录
    const sql7 = 'DELETE FROM wantlogs WHERE pid in ('+theids+') AND type = $1 RETURNING userid';//删除想要记录
    const params = [2];
    const params1 = [3];
    let b = 0;
    let c = 0;
    pg.query(sql1, params, function(err, result){//是否有已经被删除的体验
        if(err){
            callback (err, null);
        }else if (result.rowCount > 0){
            callback (err, 'exit_del');//存在已经被删除的体验
        }else {
            pg.connect(function (err, client, done){
                client.query(sqlStart, function(){
                    client.query(sql2, params, function(err, result){//删除体验(修改状态为删除)
                        if(err){
                            log.error('删除体验(修改状态为删除)报错');
                            client.query(sqlRollback, function(errRollback){
                                log.error('回滚');
                                if(errRollback){
                                    log.error('回滚失败');
                                }
                                done();
                                callback (err, null);
                            })
                        }else {
                            client.query(sql6, params1, function(err, result){
                                if(err){
                                    log.error('删除拥有记录失败');
                                    client.query(sqlRollback, function (errRollback) {
                                        if(errRollback){
                                            log.error('回滚失败');
                                        }
                                        done();
                                        callback (err, null);
                                    })
                                }else {
                                    let ousers = result.rows;
                                    let ouserlen = result.rowCount;
                                    let ouids = [];
                                    ousers.forEach(function (one_user) {
                                        ouids.push(one_user.userid);
                                    })
                                    ouids = "'" + ouids.join("','") + "'";
                                    let sql10 = 'SELECT e_ownnum, id FROM users WHERE id in (' + ouids + ')';
                                    let sql11 = 'UPDATE users SET e_ownnum = $1 WHERE id = $2';
                                    if(ouserlen > 0){
                                        client.query(sql10, [], function (err, result) {
                                            if (err) {
                                                log.error('查询拥有作品用户失败');
                                                client.query(sqlRollback, function (errRollback) {
                                                    log.error('回滚');
                                                    if (errRollback) {
                                                        log.error('回滚失败');
                                                    }
                                                    done();
                                                    callback(err, null);
                                                })
                                            } else {
                                                let users = result.rows;
                                                let olen = result.rowCount;
                                                if (olen > 0) {
                                                    users.forEach(function (theuser) {
                                                        let params11 = [
                                                            theuser.e_ownnum > 0 ? (Number(theuser.e_ownnum) - 1): Number(0),
                                                            theuser.id
                                                        ];
                                                        client.query(sql11, params11, function (err, result) {
                                                            if (err || result.rowCount == 0) {
                                                                log.error('修改用户发拥有品失败');
                                                                client.query(sqlRollback, function (errRollback) {
                                                                    if (errRollback) {
                                                                        log.error('回滚失败');
                                                                    }
                                                                    done();
                                                                    callback(err, null);
                                                                })
                                                            } else {
                                                                b++;
                                                                if(b == olen){
                                                                    client.query(sql7, params1, function(err, result){
                                                                        if(err){
                                                                            log.error('删除想要记录失败');
                                                                            client.query(sqlRollback, function (errRollback) {
                                                                                if(errRollback){
                                                                                    log.error('回滚失败');
                                                                                }
                                                                                done();
                                                                                callback (err, null);
                                                                            })
                                                                        }else {
                                                                            let wusers = result.rows;
                                                                            let wlength = result.rowCount;
                                                                            let wuids = [];
                                                                            wusers.forEach(function (one_user) {
                                                                                wuids.push(one_user.userid);
                                                                            })
                                                                            wuids = "'" + wuids.join("','") + "'";
                                                                            let sql12 = 'SELECT e_wantnum, id FROM users WHERE id in (' + wuids + ')';
                                                                            let sql13 = 'UPDATE users SET e_wantnum = $1 WHERE id = $2';
                                                                            if(wlength > 0){
                                                                                client.query(sql12, [], function (err, result) {
                                                                                    if (err) {
                                                                                        log.error('查询拥有作品用户失败');
                                                                                        client.query(sqlRollback, function (errRollback) {
                                                                                            log.error('回滚');
                                                                                            if (errRollback) {
                                                                                                log.error('回滚失败');
                                                                                            }
                                                                                            done();
                                                                                            callback(err, null);
                                                                                        })
                                                                                    } else {
                                                                                        let users = result.rows;
                                                                                        let wlen = result.rowCount;
                                                                                        if (wlen > 0) {
                                                                                            users.forEach(function (theuser) {
                                                                                                let params13 = [
                                                                                                    theuser.e_wantnum > 0 ? (Number(theuser.e_wantnum) - 1): Number(0),
                                                                                                    theuser.id
                                                                                                ];
                                                                                                client.query(sql13, params13, function (err, result) {
                                                                                                    if (err || result.rowCount == 0) {
                                                                                                        log.error('修改用户发拥有品失败');
                                                                                                        client.query(sqlRollback, function (errRollback) {
                                                                                                            if (errRollback) {
                                                                                                                log.error('回滚失败');
                                                                                                            }
                                                                                                            done();
                                                                                                            callback(err, null);
                                                                                                        })
                                                                                                    } else {
                                                                                                        c++;
                                                                                                        if(c == wlen){
                                                                                                            client.query(sqlCommit, function (errCommit) {
                                                                                                                if (errCommit) {
                                                                                                                    client.query(sqlRollback, function (errRollback) {
                                                                                                                        if (errRollback) {
                                                                                                                            log.error('回滚失败');
                                                                                                                        }
                                                                                                                        done();
                                                                                                                        callback(err, null);
                                                                                                                    });
                                                                                                                } else {
                                                                                                                    done();
                                                                                                                    return callback(err, result);//删除成功
                                                                                                                }
                                                                                                            });
                                                                                                        }
                                                                                                    }
                                                                                                })
                                                                                            })

                                                                                        }
                                                                                        if (wlen == 0) {
                                                                                            client.query(sqlCommit, function (errCommit) {
                                                                                                if (errCommit) {
                                                                                                    client.query(sqlRollback, function (errRollback) {
                                                                                                        if (errRollback) {
                                                                                                            log.error('回滚失败');
                                                                                                        }
                                                                                                        done();
                                                                                                        callback(err, null);
                                                                                                    });
                                                                                                } else {
                                                                                                    done();
                                                                                                    return callback(err, result);//删除成功
                                                                                                }
                                                                                            });
                                                                                        }
                                                                                    }
                                                                                })
                                                                            }else {
                                                                                client.query(sqlCommit, function (errCommit) {
                                                                                    if (errCommit) {
                                                                                        client.query(sqlRollback, function (errRollback) {
                                                                                            if (errRollback) {
                                                                                                log.error('回滚失败');
                                                                                            }
                                                                                            done();
                                                                                            callback(err, null);
                                                                                        });
                                                                                    } else {
                                                                                        done();
                                                                                        return callback(err, result);//删除成功
                                                                                    }
                                                                                });
                                                                            }

                                                                        }
                                                                    })
                                                                }
                                                            }
                                                        })
                                                    })

                                                }
                                                if (olen == 0) {
                                                    client.query(sql7, params1, function(err, result){
                                                        if(err){
                                                            log.error('删除想要记录失败');
                                                            client.query(sqlRollback, function (errRollback) {
                                                                if(errRollback){
                                                                    log.error('回滚失败');
                                                                }
                                                                done();
                                                                callback (err, null);
                                                            })
                                                        }else {
                                                            let wusers = result.rows;
                                                            let wlength = result.rowCount;
                                                            let wuids = [];
                                                            wusers.forEach(function (one_user) {
                                                                wuids.push(one_user.userid);
                                                            })
                                                            wuids = "'" + wuids.join("','") + "'";
                                                            let sql12 = 'SELECT e_wantnum, id FROM users WHERE id in (' + wuids + ')';
                                                            let sql13 = 'UPDATE users SET e_wantnum = $1 WHERE id = $2';
                                                            if(wlength > 0){
                                                                client.query(sql12, [], function (err, result) {
                                                                    if (err) {
                                                                        log.error('查询拥有作品用户失败');
                                                                        client.query(sqlRollback, function (errRollback) {
                                                                            log.error('回滚');
                                                                            if (errRollback) {
                                                                                log.error('回滚失败');
                                                                            }
                                                                            done();
                                                                            callback(err, null);
                                                                        })
                                                                    } else {
                                                                        let users = result.rows;
                                                                        let wlen = result.rowCount;
                                                                        if (wlen > 0) {
                                                                            users.forEach(function (theuser) {
                                                                                let params13 = [
                                                                                    theuser.e_wantnum > 0 ? (Number(theuser.e_wantnum) - 1): Number(0),
                                                                                    theuser.id
                                                                                ];
                                                                                client.query(sql13, params13, function (err, result) {
                                                                                    if (err || result.rowCount == 0) {
                                                                                        log.error('修改用户发拥有品失败');
                                                                                        client.query(sqlRollback, function (errRollback) {
                                                                                            if (errRollback) {
                                                                                                log.error('回滚失败');
                                                                                            }
                                                                                            done();
                                                                                            callback(err, null);
                                                                                        })
                                                                                    } else {
                                                                                        c++;
                                                                                        if(c == wlen){
                                                                                            client.query(sqlCommit, function (errCommit) {
                                                                                                if (errCommit) {
                                                                                                    client.query(sqlRollback, function (errRollback) {
                                                                                                        if (errRollback) {
                                                                                                            log.error('回滚失败');
                                                                                                        }
                                                                                                        done();
                                                                                                        callback(err, null);
                                                                                                    });
                                                                                                } else {
                                                                                                    done();
                                                                                                    return callback(err, result);//删除成功
                                                                                                }
                                                                                            });
                                                                                        }
                                                                                    }
                                                                                })
                                                                            })

                                                                        }
                                                                        if (wlen == 0) {
                                                                            client.query(sqlCommit, function (errCommit) {
                                                                                if (errCommit) {
                                                                                    client.query(sqlRollback, function (errRollback) {
                                                                                        if (errRollback) {
                                                                                            log.error('回滚失败');
                                                                                        }
                                                                                        done();
                                                                                        callback(err, null);
                                                                                    });
                                                                                } else {
                                                                                    done();
                                                                                    return callback(err, result);//删除成功
                                                                                }
                                                                            });
                                                                        }
                                                                    }
                                                                })
                                                            }else {
                                                                client.query(sqlCommit, function (errCommit) {
                                                                    if (errCommit) {
                                                                        client.query(sqlRollback, function (errRollback) {
                                                                            if (errRollback) {
                                                                                log.error('回滚失败');
                                                                            }
                                                                            done();
                                                                            callback(err, null);
                                                                        });
                                                                    } else {
                                                                        done();
                                                                        return callback(err, result);//删除成功
                                                                    }
                                                                });
                                                            }

                                                        }
                                                    })
                                                }
                                            }
                                        })
                                    }else {
                                        client.query(sql7, params1, function(err, result){
                                            if(err){
                                                log.error('删除想要记录失败');
                                                client.query(sqlRollback, function (errRollback) {
                                                    if(errRollback){
                                                        log.error('回滚失败');
                                                    }
                                                    done();
                                                    callback (err, null);
                                                })
                                            }else {
                                                let wusers = result.rows;
                                                let wlength = result.rowCount;
                                                let wuids = [];
                                                wusers.forEach(function (one_user) {
                                                    wuids.push(one_user.userid);
                                                })
                                                wuids = "'" + wuids.join("','") + "'";
                                                let sql12 = 'SELECT e_wantnum, id FROM users WHERE id in (' + wuids + ')';
                                                let sql13 = 'UPDATE users SET e_wantnum = $1 WHERE id = $2';
                                                if(wlength > 0){
                                                    client.query(sql12, [], function (err, result) {
                                                        if (err) {
                                                            log.error('查询拥有作品用户失败');
                                                            client.query(sqlRollback, function (errRollback) {
                                                                log.error('回滚');
                                                                if (errRollback) {
                                                                    log.error('回滚失败');
                                                                }
                                                                done();
                                                                callback(err, null);
                                                            })
                                                        } else {
                                                            let users = result.rows;
                                                            let wlen = result.rowCount;
                                                            if (wlen > 0) {
                                                                users.forEach(function (theuser) {
                                                                    let params13 = [
                                                                        theuser.e_wantnum > 0 ? (Number(theuser.e_wantnum) - 1): Number(0),
                                                                        theuser.id
                                                                    ];
                                                                    client.query(sql13, params13, function (err, result) {
                                                                        if (err || result.rowCount == 0) {
                                                                            log.error('修改用户发拥有品失败');
                                                                            client.query(sqlRollback, function (errRollback) {
                                                                                if (errRollback) {
                                                                                    log.error('回滚失败');
                                                                                }
                                                                                done();
                                                                                callback(err, null);
                                                                            })
                                                                        } else {
                                                                            c++;
                                                                            if(c == wlen){
                                                                                client.query(sqlCommit, function (errCommit) {
                                                                                    if (errCommit) {
                                                                                        client.query(sqlRollback, function (errRollback) {
                                                                                            if (errRollback) {
                                                                                                log.error('回滚失败');
                                                                                            }
                                                                                            done();
                                                                                            callback(err, null);
                                                                                        });
                                                                                    } else {
                                                                                        done();
                                                                                        return callback(err, result);//删除成功
                                                                                    }
                                                                                });
                                                                            }
                                                                        }
                                                                    })
                                                                })

                                                            }
                                                            if (wlen == 0) {
                                                                client.query(sqlCommit, function (errCommit) {
                                                                    if (errCommit) {
                                                                        client.query(sqlRollback, function (errRollback) {
                                                                            if (errRollback) {
                                                                                log.error('回滚失败');
                                                                            }
                                                                            done();
                                                                            callback(err, null);
                                                                        });
                                                                    } else {
                                                                        done();
                                                                        return callback(err, result);//删除成功
                                                                    }
                                                                });
                                                            }
                                                        }
                                                    })
                                                }else {
                                                    client.query(sqlCommit, function (errCommit) {
                                                        if (errCommit) {
                                                            client.query(sqlRollback, function (errRollback) {
                                                                if (errRollback) {
                                                                    log.error('回滚失败');
                                                                }
                                                                done();
                                                                callback(err, null);
                                                            });
                                                        } else {
                                                            done();
                                                            return callback(err, result);//删除成功
                                                        }
                                                    });
                                                }

                                            }
                                        })
                                    }

                                }
                            })
                        }
                    });
                });
            });
        }
    });
};
/**
 * app查询现场列表
 * @param params
 * @param callback
 */
//const app_list_sql1 = 'SELECT id, title, "desc", imgurl, want, own, path, username, point, userid FROM experience WHERE status = $1 AND del = $2 ORDER BY createtime DESC LIMIT $3 OFFSET $4';//没有限制条件，只有分页信息
// const app_list_sql2 = 'SELECT createtime FROM wantlogs WHERE createtime > $2 AND pid = $1 AND type = $3 ORDER BY createtime DESC LIMIT 1 OFFSET 0';//查现场最后一次想要时间
const app_list_sql2 = 'SELECT w.createtime, w.userid, u.headurl FROM wantlogs w, users u WHERE w.userid=u.id AND w.pid = $1 AND w.createtime > $2 AND w.type = $3 ORDER BY w.createtime DESC LIMIT 1 OFFSET 0';//查找想要记录中最后一次想要时间
exports.list_app  = function(params, args, callback){
    let app_list_sql1 = 'SELECT id, title, "desc", imgurl, want, own, path, username, point, userid  FROM experience WHERE  1=1';//没有限制条件，只有分页信息
    let number = 1;
    const p_sql4 = 'SELECT * FROM wantlogs WHERE userid = $1 AND type = $2 AND pid = $3';
    const p_sql5 = 'SELECT * FROM ownlogs WHERE userid = $1 AND type = $2 AND pid = $3';
    if(args.userid){
        app_list_sql1 +=' AND author = $'+number;
        number++;
    }
    if(args.eid){
        app_list_sql1 +=' AND id != $'+number;
        number++;
    }
    app_list_sql1 +=' AND  status = $'+number+' AND del = $'+(number+1)+' ORDER BY createtime DESC LIMIT $'+(number+2)+' OFFSET $'+(number+3);
    let date1 =  new Date().getTime();
    let date2 = new Date(date1 - 1000 * 60 * 60 * 6);//6小时之内
    var info;
    let infos = [];
    pg.query(app_list_sql1, params, function(err, result){//执行查找
        if(err){
            callback (err, null);//报错
        }else {
            let experiences = result.rows;
            let len = experiences.length;
            let a = 0;
            if(len > 0 ) {
                pg.connect(function (err, client, done) { //获取数据库链接
                    client.query(sqlStart, function () { //开启事务
                        async.mapSeries(experiences, function (experience, callback) {
                        let paramswant = [
                            experience.id,
                            date2,
                            3
                        ];
                        const params4 = [
                            args.myid,
                            3,
                            experience.id
                        ]
                        pg.query(app_list_sql2, paramswant, function (err, result) {
                            if (err) {
                                callback(err, result);
                            } else if (result.rowCount == 0) {
                                let last_time = null;
                                let headurl = null;
                                pg.query(p_sql4, params4, function (err, result) {
                                    if (err) {
                                        callback(err, result);
                                    } else {
                                        let u_want = 0;
                                        u_want = result.rowCount;
                                        pg.query(p_sql5, params4, function (err, result) {
                                            if (err) {
                                                callback(err, result);
                                            } else {
                                                let u_own = result.rowCount;
                                                info = {
                                                    id: experience.id,
                                                    title: experience.title,
                                                    desc: experience.desc,
                                                    imgurl: experience.imgurl,
                                                    want: experience.want,
                                                    own: experience.own,
                                                    path: experience.path,
                                                    username: experience.username,
                                                    point: experience.point,
                                                    userid: experience.userid,
                                                    headurl: headurl,
                                                    last_time: last_time,
                                                    u_want: u_want,
                                                    u_own: u_own
                                                };
                                                a++;
                                                infos.push(info);
                                                callback(err, infos);//结果

                                                ;
                                            }
                                        })
                                    }
                                })
                            } else {
                                let last_time = result.rows[0].createtime;
                                let headurl = result.rows[0].headurl;
                                pg.query(p_sql4, params4, function (err, result) {
                                    if (err) {
                                        callback(err, result);
                                    } else {
                                        let u_want = 0;
                                        u_want = result.rowCount;
                                        pg.query(p_sql5, params4, function (err, result) {
                                            if (err) {
                                                callback(err, result);
                                            } else {
                                                let u_own = result.rowCount;
                                                info = {
                                                    id: experience.id,
                                                    title: experience.title,
                                                    desc: experience.desc,
                                                    imgurl: experience.imgurl,
                                                    want: experience.want,
                                                    own: experience.own,
                                                    path: experience.path,
                                                    username: experience.username,
                                                    point: experience.point,
                                                    userid: experience.userid,
                                                    headurl: headurl,
                                                    last_time: last_time,
                                                    u_want: u_want,
                                                    u_own: u_own
                                                };
                                                a++;
                                                infos.push(info);
                                                callback(err, infos);//结果

                                                ;
                                            }
                                        })
                                    }
                                })
                            }
                        });
                    },
                    function (err, n) { //循环完毕提交事务
                        if (err) {
                            log.error(err);
                            log.error('回滚.............');
                            client.query(sqlRollback, function (errRollback) {
                                if (errRollback) {
                                    log.error('回滚失败');
                                    log.error(errRollback);
                                }
                                done();
                                callback(errRollback, null);
                            });
                        } else {
                            log.debug('提交.............');
                            client.query(sqlCommit, function (errCommit) {
                                if (errCommit) {
                                    client.query(sqlRollback, function (errRollback) {
                                        if (errRollback) {
                                            console.log('回滚失败');
                                            log.error(errRollback);
                                        }
                                        done(); //关闭链接
                                        callback(errCommit, infos);
                                    });
                                } else {
                                    done();
                                    callback(errCommit, infos);
                                }

                            });
                        }
                    })
                 })
            })
            }else {
                callback (err, infos);//没有现场
            }

        }
    });
};
/**
 * app查询现场列表
 * @param params
 * @param callback
 */
//const app_list_sql1 = 'SELECT id, title, "desc", imgurl, want, own, path, username, point, userid FROM experience WHERE status = $1 AND del = $2 ORDER BY createtime DESC LIMIT $3 OFFSET $4';//没有限制条件，只有分页信息
// const app_list_sql2 = 'SELECT createtime FROM wantlogs WHERE createtime > $2 AND pid = $1 AND type = $3 ORDER BY createtime DESC LIMIT 1 OFFSET 0';//查现场最后一次想要时间
const others_sql2 = 'SELECT w.createtime, w.userid, u.headurl FROM wantlogs w, users u WHERE w.userid=u.id AND w.pid = $1 AND w.createtime > $2 AND w.type = $3 ORDER BY w.createtime DESC LIMIT 1 OFFSET 0';//查找想要记录中最后一次想要时间
exports.others  = function(params, args, req, callback){
    let others_sql1 = 'SELECT id, title, "desc", imgurl, want, own, path, username, point, userid  FROM experience WHERE id != $1AND  status = $2 AND del = $3 ORDER BY want DESC, own DESC, createtime DESC LIMIT 10 OFFSET 0';//没有限制条件，只有分页信息
    const p_sql4 = 'SELECT * FROM wantlogs WHERE userid = $1 AND type = $2 AND pid = $3';
    const p_sql5 = 'SELECT * FROM ownlogs WHERE userid = $1 AND type = $2 AND pid = $3';
    let date1 =  new Date().getTime();
    let date2 = new Date(date1 - 1000 * 60 * 60 * 6);//6小时之内
    var info;
    let infos = [];
    pg.query(others_sql1, params, function(err, result){//执行查找
        if(err){
            callback (err, null);//报错
        }else {
            let experiences = result.rows;
            let len = experiences.length;
            let a = 0;
            if(len > 0 ) {
                pg.connect(function (err, client, done) { //获取数据库链接
                    client.query(sqlStart, function () { //开启事务
                        async.mapSeries(experiences, function (experience, callback) {
                        let paramswant = [
                            experience.id,                            date2,
                            3
                        ];
                        const params4 = [
                            req.user.id,
                            3,
                            experience.id
                        ]
                        pg.query(others_sql2, paramswant, function (err, result) {
                            if (err) {
                                callback(err, result);
                            } else if (result.rowCount == 0) {
                                let last_time = null;
                                let headurl = null;
                                pg.query(p_sql4, params4, function (err, result) {
                                    if (err) {
                                        callback(err, result);
                                    } else {
                                        let u_want = 0;
                                        u_want = result.rowCount;
                                        pg.query(p_sql5, params4, function (err, result) {
                                            if (err) {
                                                callback(err, result);
                                            } else {
                                                let u_own = result.rowCount;
                                                info = {
                                                    id: experience.id,
                                                    title: experience.title,
                                                    desc: experience.desc,
                                                    imgurl: experience.imgurl,
                                                    want: experience.want,
                                                    own: experience.own,
                                                    path: experience.path,
                                                    username: experience.username,
                                                    point: experience.point,
                                                    userid: experience.userid,
                                                    headurl: headurl,
                                                    last_time: last_time,
                                                    u_want: u_want,
                                                    u_own: u_own
                                                };
                                                a++;
                                                infos.push(info);
                                                callback(err, infos);//结果

                                                ;
                                            }
                                        })
                                    }
                                })
                            } else {
                                let last_time = result.rows[0].createtime;
                                let headurl = result.rows[0].headurl;
                                pg.query(p_sql4, params4, function (err, result) {
                                    if (err) {
                                        callback(err, result);
                                    } else {
                                        let u_want = 0;
                                        u_want = result.rowCount;
                                        pg.query(p_sql5, params4, function (err, result) {
                                            if (err) {
                                                callback(err, result);
                                            } else {
                                                let u_own = result.rowCount;
                                                info = {
                                                    id: experience.id,
                                                    title: experience.title,
                                                    imgurl: experience.imgurl,
                                                    content: experience.content,
                                                    authname: experience.authname,
                                                    abstract: experience.abstract,
                                                    want: experience.want,
                                                    own: experience.own,
                                                    point: experience.point,
                                                    headurl: headurl,
                                                    last_time: last_time,
                                                    u_want: u_want,
                                                    u_own: u_own
                                                };
                                                a++;
                                                infos.push(info);
                                                callback(err, infos);//结果

                                                ;
                                            }
                                        })
                                    }
                                })
                            }
                        });
                    },
                    function (err, n) { //循环完毕提交事务
                        if (err) {
                            log.error(err);
                            log.error('回滚.............');
                            client.query(sqlRollback, function (errRollback) {
                                if (errRollback) {
                                    log.error('回滚失败');
                                    log.error(errRollback);
                                }
                                done();
                                callback(errRollback, null);
                            });
                        } else {
                            log.debug('提交.............');
                            client.query(sqlCommit, function (errCommit) {
                                if (errCommit) {
                                    client.query(sqlRollback, function (errRollback) {
                                        if (errRollback) {
                                            console.log('回滚失败');
                                            log.error(errRollback);
                                        }
                                        done(); //关闭链接
                                        callback(errCommit, infos);
                                    });
                                } else {
                                    done();
                                    callback(errCommit, infos);
                                }

                            });
                        }
                    })
                 })
            })
            }else {
                callback (err, infos);//没有现场
            }

        }
    });
};
/**
 * 前后台现场详情
 * @param params
 * @param callback
 */
const info_sql1 = 'SELECT e.id, e.title, e."desc", e.imgurl, e.want, e.own, e.path, u.username, e.point, e.createtime, e.userid , u.headurl FROM experience e, users u WHERE u.id = e.userid AND e.id = $1 AND e.status = $2 AND e.del = $3';//查找的sql语句
const info_sql2 = 'SELECT u.headurl, u.username, w.userid FROM users u, wantlogs w WHERE u.id = w.userid AND w.pid = $1 AND w.type = $2 ORDER BY w.createtime DESC';
const info_sql3 = 'SELECT u.headurl, u.username, o.userid FROM users u, ownlogs o WHERE u.id = o.userid AND o.pid = $1 AND o.type = $2 ORDER BY o.createtime DESC';
const info_sql4 = 'SELECT * FROM wantlogs WHERE userid = $1 AND pid = $2 AND type = $3';
const info_sql5 = 'SELECT * FROM ownlogs WHERE userid = $1 AND pid = $2 AND type = $3';
exports.info = function(params, args, callback) {
    let infos = {};
    const params1 = [
        params[0],
        3
    ];
    const params4 = [
        args.userid,
        args.id,
        3
    ]
    pg.query(info_sql1, params, function (err, result) {
        if (err) {//报错
            callback(err, null);
        } else if (result.rowCount == 0) {
            callback(err, 'null');//没有这篇体验
        } else {
            let experience = result.rows[0];
            infos.experience = experience;
            pg.query(info_sql2, params1, function (err, result) {
                if (err) {//报错
                    callback(err, null);
                } else {
                    let e_want = result.rows;
                    infos.e_want=e_want;
                    pg.query(info_sql3, params1, function(err, result){
                        if(err){//报错
                            callback (err, null);
                        }else {
                            let e_own = result.rows;
                            infos.e_own=e_own;
                            pg.query(info_sql4, params4, function(err, result){
                                if(err){//报错
                                    callback (err, null);
                                }else {
                                    let u_want = result.rowCount;
                                    infos.u_want=u_want;
                                    pg.query(info_sql5, params4, function(err, result){
                                        if(err){//报错
                                            callback (err, null);
                                        }else {
                                            let u_own = result.rowCount;
                                            infos.u_own=u_own;
                                            callback (err, infos);//执行结束
                                        }
                                    });
                                }
                            });
                        }
                    });
                }
            });
        }
    });
};

/**
 * app用户想要体验
 * @param params
 * @param args
 * @param callback
 */
const want_sql1 = 'SELECT * FROM experience WHERE id = $1 AND del = $2';//查体验
const want_sql2 = 'SELECT * FROM wantlogs WHERE pid = $1 AND userid = $2 AND type = $3';//查是否已经添加想要
const want_sql3 = 'INSERT INTO wantlogs (pid, userid, type, createtime) VALUES ($1, $2, $3, $4)';//添加想要记录
const want_sql4 = 'SELECT * FROM users WHERE id = $1';//查用户想要作品数
const want_sql5 = 'UPDATE users SET  e_wantnum = $1 WHERE id = $2';//修改用户想要作品数
const want_sql6 = 'UPDATE experience SET  want = $1 WHERE id = $2';//修改作品想要数
exports.want = function(params, args, req, callback){
    const params1 = [
        args.experienceid,
        1
    ];
    const params2 = [
        args.experienceid,
        req.user.id,
        3
    ];
    const params4 = [
        req.user.id
    ];
    pg.query(want_sql1, params1, function(err, result){//执行查体验操作
        if(err){
            callback (err, null);//报错
        }else if(result.rowCount == 0){
            callback (err, 'null_experience');//没有这个体验
        }else {
            let experience = result.rows[0];
            const params6 = [
                experience.want>0?Number(experience.want)+1:1,//体验想要人数
                experience.id//体验id
            ];
            pg.query(want_sql2, params2, function(err, result){//执行查想要记录
                if(err){
                    callback (err, null);//报错
                }else if(result.rowCount > 0){
                    callback (err, 'exit_logs');//该用户已经对这个体验添加想要
                }else {
                    pg.connect(function (err, client, done) {
                        client.query(sqlStart, function () {
                            client.query(want_sql3, params, function (err, result) {//添加想要记录
                                if (err || result.rowCount == 0) {
                                    log.error('添加想要记录失败');
                                    client.query(sqlRollback, function (errRollback) {
                                        log.error('回滚');
                                        if (errRollback) {
                                            log.error('回滚失败');
                                        }
                                        done();
                                        callback(err, null);
                                    })
                                } else {
                                    client.query(want_sql6, params6, function(err, result){//修改体验想要人数失败
                                        if(err||result.rowCount == 0){
                                            log.error('修改体验想要人数失败');
                                            client.query(sqlRollback, function(errRollback){
                                                log.error('回滚');
                                                if(errRollback){
                                                    log.error('回滚失败');
                                                }
                                                done();
                                                callback(err, null);
                                            })
                                        }else{
                                            client.query(want_sql4, params4, function(err, result){//查想要人的信息
                                                if(err||result.rowCount == 0){
                                                    log.error('查想要人的信息失败');
                                                    client.query(sqlRollback, function(errRollback){
                                                        log.error('回滚');
                                                        if(errRollback){
                                                            log.error('回滚失败');
                                                        }
                                                        done();
                                                        callback(err, null);
                                                    })
                                                }else{
                                                    let user = result.rows[0];
                                                    const params5 = [
                                                        user.a_wantnum>0?Number(user.a_wantnum)+1:1,//数量
                                                        user.id//用户id
                                                    ];
                                                    client.query(want_sql5, params5, function(err, result){//修改用户想要作品数
                                                        if(err||result.rowCount == 0){
                                                            log.error('修改用户想要作品数失败');
                                                            client.query(sqlRollback, function(errRollback){
                                                                log.error('回滚');
                                                                if(errRollback){
                                                                    log.error('回滚失败');
                                                                }
                                                                done();
                                                                callback(err, null);
                                                            })
                                                        }else{
                                                            client.query(want_sql1, params1, function(err, result){//修改用户想要作品数
                                                                if(err||result.rowCount == 0){
                                                                    log.error('修改用户想要作品数失败');
                                                                    client.query(sqlRollback, function(errRollback){
                                                                        log.error('回滚');
                                                                        if(errRollback){
                                                                            log.error('回滚失败');
                                                                        }
                                                                        done();
                                                                        callback(err, null);
                                                                    })
                                                                }else{
                                                                    let info = result.rows[0].want;
                                                                    client.query(sqlCommit, function (errCommit) {//结束进程
                                                                        if (errCommit) {
                                                                            client.query(sqlRollback, function (errRollback) {
                                                                                if (errRollback) {
                                                                                    log.error('回滚失败');
                                                                                }
                                                                                done();
                                                                                callback(err, null);
                                                                            });
                                                                        } else {
                                                                            done();
                                                                            return callback(err, info);//添加想要成功
                                                                        }
                                                                    });
                                                                }
                                                            })
                                                        }
                                                    })

                                                }
                                            })
                                        }
                                    })
                                }
                            })
                        })
                    })
                }
            });
        }
    });
};
/**
 * app用户拥有体验
 * @param params
 * @param args
 * @param callback
 */
const own_sql1 = 'SELECT * FROM experience WHERE id = $1 AND del = $2';//查体验
const own_sql2 = 'SELECT * FROM ownlogs WHERE pid = $1 AND userid = $2 AND type = $3';//查是否已经添加拥有
const own_sql3 = 'INSERT INTO ownlogs (pid, userid, type, createtime) VALUES ($1, $2, $3, $4)';//添加拥有记录
const own_sql4 = 'SELECT * FROM users WHERE id = $1';//查用户拥有作品数
const own_sql5 = 'UPDATE users SET  e_ownnum = $1 WHERE id = $2';//修改用户拥有作品数
const own_sql6 = 'UPDATE experience SET  own = $1 WHERE id = $2';//修改作品拥有数
exports.own = function(params, args, req, callback){
    const params1 = [
        args.experienceid,
        1
    ];
    const params2 = [
        args.experienceid,
        req.user.id,
        3
    ];
    const params4 = [
        req.user.id
    ];
    pg.query(own_sql1, params1, function(err, result){//执行查体验操作
        if(err){
            callback (err, null);//报错
        }else if(result.rowCount == 0){
            callback (err, 'null_experience');//没有这个体验
        }else {
            let experience = result.rows[0];
            const params6 = [
                experience.own>0?Number(experience.own)+1:1,//体验想要人数
                experience.id//体验id
            ];
            pg.query(own_sql2, params2, function(err, result){//执行查拥有记录
                if(err){
                    callback (err, null);//报错
                }else if(result.rowCount > 0){
                    callback (err, 'exit_logs');//该用户已经对这个体验添加拥有
                }else {
                    pg.connect(function (err, client, done) {
                        client.query(sqlStart, function () {
                            client.query(own_sql3, params, function (err, result) {//添加拥有记录
                                if (err || result.rowCount == 0) {
                                    log.error('添加拥有记录失败');
                                    client.query(sqlRollback, function (errRollback) {
                                        log.error('回滚');
                                        if (errRollback) {
                                            log.error('回滚失败');
                                        }
                                        done();
                                        callback(err, null);
                                    })
                                } else {
                                    client.query(own_sql6, params6, function(err, result){//修改体验拥有人数失败
                                        if(err||result.rowCount == 0){
                                            log.error('修改体验拥有人数失败');
                                            client.query(sqlRollback, function(errRollback){
                                                log.error('回滚');
                                                if(errRollback){
                                                    log.error('回滚失败');
                                                }
                                                done();
                                                callback(err, null);
                                            })
                                        }else{
                                            client.query(own_sql4, params4, function(err, result){//查拥有人的信息
                                                if(err||result.rowCount == 0){
                                                    log.error('查拥有人的信息失败');
                                                    client.query(sqlRollback, function(errRollback){
                                                        log.error('回滚');
                                                        if(errRollback){
                                                            log.error('回滚失败');
                                                        }
                                                        done();
                                                        callback(err, null);
                                                    })
                                                }else{
                                                    let user = result.rows[0];
                                                    const params5 = [
                                                        user.p_ownnum>0?Number(user.p_ownnum)+1:1,//数量
                                                        user.id//用户id
                                                    ];
                                                    client.query(own_sql5, params5, function(err, result){//修改用户拥有作品数
                                                        if(err||result.rowCount == 0){
                                                            log.error('修改用户拥有作品数失败');
                                                            client.query(sqlRollback, function(errRollback){
                                                                log.error('回滚');
                                                                if(errRollback){
                                                                    log.error('回滚失败');
                                                                }
                                                                done();
                                                                callback(err, null);
                                                            })
                                                        }else{
                                                            client.query(own_sql1, params1, function(err, result){//修改用户想要作品数
                                                                if(err||result.rowCount == 0){
                                                                    log.error('修改用户想要作品数失败');
                                                                    client.query(sqlRollback, function(errRollback){
                                                                        log.error('回滚');
                                                                        if(errRollback){
                                                                            log.error('回滚失败');
                                                                        }
                                                                        done();
                                                                        callback(err, null);
                                                                    })
                                                                }else{
                                                                    let info = result.rows[0].own;
                                                                    client.query(sqlCommit, function (errCommit) {//结束进程
                                                                        if (errCommit) {
                                                                            client.query(sqlRollback, function (errRollback) {
                                                                                if (errRollback) {
                                                                                    log.error('回滚失败');
                                                                                }
                                                                                done();
                                                                                callback(err, null);
                                                                            });
                                                                        } else {
                                                                            done();
                                                                            return callback(err, info);//添加想要成功
                                                                        }
                                                                    });
                                                                }
                                                            })
                                                        }
                                                    })
                                                }
                                            })
                                        }
                                    })
                                }
                            })
                        })
                    })
                }
            });
        }
    });
};

/**
 * app用户想要体验
 * @param params
 * @param args
 * @param callback
 */
const unwant_sql1 = 'SELECT * FROM experience WHERE id = $1 AND del = $2';//查体验
const unwant_sql2 = 'SELECT * FROM wantlogs WHERE pid = $1 AND userid = $2 AND type = $3';//查是否已经添加想要
const unwant_sql3 = 'DELETE FROM wantlogs WHERE pid = $1 AND userid = $2 AND type = $3';//添加想要记录
const unwant_sql4 = 'SELECT * FROM users WHERE id = $1';//查用户想要作品数
const unwant_sql5 = 'UPDATE users SET  e_wantnum = $1 WHERE id = $2';//修改用户想要作品数
const unwant_sql6 = 'UPDATE experience SET  want = $1 WHERE id = $2';//修改作品想要数
exports.unwant = function(params, args, req, callback){
    const params1 = [
        args.experienceid,
        1
    ];
    const params2 = [
        args.experienceid,
        req.user.id,
        3
    ];
    const params4 = [
        req.user.id
    ];
    pg.query(unwant_sql1, params1, function(err, result){//执行查照片操作
        if(err){
            callback (err, null);//报错
        }else if(result.rowCount == 0){
            callback (err, 'null_picture');//没有这个体验
        }else {
            let experience = result.rows[0];
            const params6 = [
                experience.want>1?Number(experience.want)-1:0,//照片想要人数
                experience.id//照片id
            ];
            pg.query(unwant_sql2, params2, function(err, result){//执行查想要记录
                if(err){
                    callback (err, null);//报错
                }else if(result.rowCount == 0){
                    callback (err, 'null_logs');//该用户没有对这个体验添加想要
                }else {
                    pg.connect(function (err, client, done) {
                        client.query(sqlStart, function () {
                            client.query(unwant_sql3, params, function (err, result) {//添加想要记录
                                if (err || result.rowCount == 0) {
                                    log.error('删除想要记录失败');
                                    client.query(sqlRollback, function (errRollback) {
                                        log.error('回滚');
                                        if (errRollback) {
                                            log.error('回滚失败');
                                        }
                                        done();
                                        callback(err, null);
                                    })
                                } else {
                                    client.query(unwant_sql6, params6, function(err, result){//修改照片想要人数失败
                                        if(err||result.rowCount == 0){
                                            log.error('修改照片想要人数失败');
                                            client.query(sqlRollback, function(errRollback){
                                                log.error('回滚');
                                                if(errRollback){
                                                    log.error('回滚失败');
                                                }
                                                done();
                                                callback(err, null);
                                            })
                                        }else{
                                            client.query(unwant_sql4, params4, function(err, result){//查想要人的信息
                                                if(err||result.rowCount == 0){
                                                    log.error('查想要人的信息失败');
                                                    client.query(sqlRollback, function(errRollback){
                                                        log.error('回滚');
                                                        if(errRollback){
                                                            log.error('回滚失败');
                                                        }
                                                        done();
                                                        callback(err, null);
                                                    })
                                                }else{
                                                    let user = result.rows[0];
                                                    const params5 = [
                                                        user.e_wantnum>1?Number(user.e_wantnum)-1:0,//数量
                                                        user.id//用户id
                                                    ];
                                                    client.query(unwant_sql5, params5, function(err, result){//修改用户想要作品数
                                                        if(err||result.rowCount == 0){
                                                            log.error('修改用户想要作品数失败');
                                                            client.query(sqlRollback, function(errRollback){
                                                                log.error('回滚');
                                                                if(errRollback){
                                                                    log.error('回滚失败');
                                                                }
                                                                done();
                                                                callback(err, null);
                                                            })
                                                        }else{
                                                            client.query(unwant_sql1, params1, function(err, result){//修改用户想要作品数
                                                                if(err||result.rowCount == 0){
                                                                    log.error('修改用户想要作品数失败');
                                                                    client.query(sqlRollback, function(errRollback){
                                                                        log.error('回滚');
                                                                        if(errRollback){
                                                                            log.error('回滚失败');
                                                                        }
                                                                        done();
                                                                        callback(err, null);
                                                                    })
                                                                }else{
                                                                    let info = result.rows[0].want;
                                                                    client.query(sqlCommit, function (errCommit) {//结束进程
                                                                        if (errCommit) {
                                                                            client.query(sqlRollback, function (errRollback) {
                                                                                if (errRollback) {
                                                                                    log.error('回滚失败');
                                                                                }
                                                                                done();
                                                                                callback(err, null);
                                                                            });
                                                                        } else {
                                                                            done();
                                                                            return callback(err, info);//添加想要成功
                                                                        }
                                                                    });
                                                                }
                                                            })
                                                        }
                                                    })

                                                }
                                            })
                                        }
                                    })
                                }
                            })
                        })
                    })
                }
            });
        }
    });
};
/**
 * app用户拥有体验
 * @param params
 * @param args
 * @param callback
 */
const unown_sql1 = 'SELECT * FROM experience WHERE id = $1 AND del = $2';//查体验
const unown_sql2 = 'SELECT * FROM ownlogs WHERE pid = $1 AND userid = $2 AND type = $3';//查是否已经添加拥有
const unown_sql3 = 'DELETE FROM ownlogs WHERE pid = $1 AND userid = $2 AND type = $3';//添加拥有记录
const unown_sql4 = 'SELECT * FROM users WHERE id = $1';//查用户拥有作品数
const unown_sql5 = 'UPDATE users SET  e_ownnum = $1 WHERE id = $2';//修改用户拥有作品数
const unown_sql6 = 'UPDATE experience SET  own = $1 WHERE id = $2';//修改作品拥有数
exports.unown = function(params, args, req, callback){
    const params1 = [
        args.experienceid,
        1
    ];
    const params2 = [
        args.experienceid,
        req.user.id,
        3
    ];
    const params4 = [
        req.user.id
    ];
    pg.query(unown_sql1, params1, function(err, result){//执行查照片操作
        if(err){
            callback (err, null);//报错
        }else if(result.rowCount == 0){
            callback (err, 'null_picture');//没有这个体验
        }else {
            let experience = result.rows[0];
            pg.query(unown_sql2, params2, function(err, result){//执行查拥有记录
                if(err){
                    callback (err, null);//报错
                }else if(result.rowCount == 0){
                    callback (err, 'null_logs');//该用户已经对这个体验添加拥有
                }else {
                    pg.connect(function (err, client, done) {
                        client.query(sqlStart, function () {
                            client.query(unown_sql3, params, function (err, result) {//添加拥有记录
                                if (err || result.rowCount == 0) {
                                    log.error('添加拥有记录失败');
                                    client.query(sqlRollback, function (errRollback) {
                                        log.error('回滚');
                                        if (errRollback) {
                                            log.error('回滚失败');
                                        }
                                        done();
                                        callback(err, null);
                                    })
                                } else {
                                    const params6 = [
                                        experience.own>1?Number(experience.own)-1:0,//照片想要人数
                                        experience.id//照片id
                                    ];
                                    client.query(unown_sql6, params6, function(err, result){//修改照片拥有人数失败
                                        if(err||result.rowCount == 0){
                                            log.error('修改照片拥有人数失败');
                                            client.query(sqlRollback, function(errRollback){
                                                log.error('回滚');
                                                if(errRollback){
                                                    log.error('回滚失败');
                                                }
                                                done();
                                                callback(err, null);
                                            })
                                        }else{
                                            client.query(unown_sql4, params4, function(err, result){//查拥有人的信息
                                                if(err||result.rowCount == 0){
                                                    log.error('查拥有人的信息失败');
                                                    client.query(sqlRollback, function(errRollback){
                                                        log.error('回滚');
                                                        if(errRollback){
                                                            log.error('回滚失败');
                                                        }
                                                        done();
                                                        callback(err, null);
                                                    })
                                                }else{
                                                    let user = result.rows[0];
                                                    const params5 = [
                                                        user.e_ownnum>0?Number(user.e_ownnum)-1:0,//数量
                                                        user.id//用户id
                                                    ];
                                                    client.query(unown_sql5, params5, function(err, result){//修改用户拥有作品数
                                                        if(err||result.rowCount == 0){
                                                            log.error('修改用户拥有作品数失败');
                                                            client.query(sqlRollback, function(errRollback){
                                                                log.error('回滚');
                                                                if(errRollback){
                                                                    log.error('回滚失败');
                                                                }
                                                                done();
                                                                callback(err, null);
                                                            })
                                                        }else{
                                                            client.query(unown_sql1, params1, function(err, result){//修改用户想要作品数
                                                                if(err||result.rowCount == 0){
                                                                    log.error('修改用户想要作品数失败');
                                                                    client.query(sqlRollback, function(errRollback){
                                                                        log.error('回滚');
                                                                        if(errRollback){
                                                                            log.error('回滚失败');
                                                                        }
                                                                        done();
                                                                        callback(err, null);
                                                                    })
                                                                }else{
                                                                    let info = result.rows[0].own;
                                                                    client.query(sqlCommit, function (errCommit) {//结束进程
                                                                        if (errCommit) {
                                                                            client.query(sqlRollback, function (errRollback) {
                                                                                if (errRollback) {
                                                                                    log.error('回滚失败');
                                                                                }
                                                                                done();
                                                                                callback(err, null);
                                                                            });
                                                                        } else {
                                                                            done();
                                                                            return callback(err, info);//添加想要成功
                                                                        }
                                                                    });
                                                                }
                                                            })
                                                        }
                                                    })
                                                }
                                            })
                                        }
                                    })
                                }
                            })
                        })
                    })
                }
            });
        }
    });
};
